package service;

import common.Pagination;
import common.StringUtils;
import dao.CroPropagandaDao;
import entity.CroPropaganda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/14 0014.
 */
@Service
public class CroPropagandaService {
    @Autowired
    CroPropagandaDao croPropagandaDao;

    /*
   * 分页查找
   * */
    @Transactional(readOnly = true)
    public Pagination<CroPropaganda> findAllUser(int index, int size) {
        String hql = "from CroPropaganda where del_flag='0'";
        Pagination<CroPropaganda> croUserList = croPropagandaDao.findPagination(hql, index, size);
        return croUserList;
    }

    /*根据id查找单个信息*/
    @Transactional(readOnly = true)
    public CroPropaganda FindPropagandaByid(String id) {
        CroPropaganda croPropaganda = croPropagandaDao.getById(id);
        return croPropaganda;
    }

    /*删除*/
    @Transactional(readOnly = false)
    public boolean deletePropaganda(String id) {
        try {
            CroPropaganda croPropaganda = croPropagandaDao.getById(id);
            croPropaganda.setDel_flag("1");
            croPropagandaDao.update(croPropaganda);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 根据属性来查找
    * */
    @Transactional(readOnly = true)
    public Pagination<CroPropaganda> FindUserByProperty(CroPropaganda propagandaVo, int index, int size){
        String hql="from CroPropaganda where del_flag='0' ";
        StringBuffer sb=new StringBuffer();
        sb.append(hql);
        if(StringUtils.isNotBlank(propagandaVo.getPropagandaname())){
            sb.append(" and description like '%"+propagandaVo.getPropagandaname()+"%'");
        }
        if(StringUtils.isNotBlank(propagandaVo.getType())){
            sb.append(" and type='"+propagandaVo.getType()+"'");
        }
        Pagination<CroPropaganda> croPropaganda=croPropagandaDao.findPagination(sb.toString(),index,size);
        return croPropaganda;
    }



    /*添加*/
    @Transactional(readOnly = false)
    public boolean addPropaganda(CroPropaganda croPropaganda) {
        try {
            croPropagandaDao.save(croPropaganda);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /*
    修改
    */
    @Transactional(readOnly = false)
    public boolean updatePropaganda(CroPropaganda croPropaganda) {
        try {
            croPropagandaDao.update(croPropaganda);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    /**
     * 轮播图list
     * @return list
     */
    @Transactional(readOnly = true)
    public List<CroPropaganda> listCarousel(){
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("del_flag", "0");
        maps.put("type","0");
        Pagination page = croPropagandaDao.findPaginationByPropertyInvert(maps,"createtime",1,100);
        return page.getItems();
    }

    /*友情链接*/
    @Transactional(readOnly = true)
    public List<CroPropaganda> listfriendurl(){
        Map<String,Object> map=new HashMap<>();
        map.put("del_flag", "0");
        map.put("type","1");
        List<CroPropaganda> propagandaList=croPropagandaDao.findByProperty(map);
        return propagandaList;
    }
}